﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Kiiro.Model;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;

namespace Kiiro.WebServices
{

    [Serializable]
    public class KiiroTaskStatusFlags
    {
        public bool ProjectDoesNotExist = false;
        public bool UserDoesNotExist = false;
        public bool ExceptionOccurred = false;
        public string Exception;
        public bool Success = false;
    }

    /// <summary>
    /// Summary description for KiiroDiscussion
    /// </summary>
    [WebService(Namespace = "http://mykiiro.com/task")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class KiiroTask : System.Web.Services.WebService
    {

        [WebMethod]
        public void CreateTask(string projectSiteName, string title, int percentComplete, DateTime dueDate, DateTime createdDate, DateTime modifiedDate, int createdBySPUserID, int modifiedBySPUserID, int assignedToSPUserID, out int taskID, out KiiroTaskStatusFlags status)
        {
            Guid siteCollectionId;
            string serverRelativeUrl = null;
            status = new KiiroTaskStatusFlags();
            status.Success = true;
            Work work = null;
            User createdBy = null;
            User modifiedBy = null;
            User assignedTo = null;
            taskID = 0;

            try
            {
                //try and get the site collection ID and relative URL
                SPHelpers.GetSiteCollectionIdAndServerRelativeUrl(Context.Request.Url.Scheme + "://" + Context.Request.Url.Authority + Context.Request.RawUrl, out siteCollectionId, out serverRelativeUrl);

                //check that the project url exists
                if (!WebServicesHelper.DoesProjectExist(projectSiteName, siteCollectionId, serverRelativeUrl))
                {
                    status.ProjectDoesNotExist = true;
                    status.Success = false;
                }
                else
                {

                    //get the created by and assigned to
                    createdBy = UserModel.GetOrCreateUser(createdBySPUserID, siteCollectionId, serverRelativeUrl + "/" + projectSiteName);
                    modifiedBy = UserModel.GetOrCreateUser(modifiedBySPUserID, siteCollectionId, serverRelativeUrl + "/" + projectSiteName);
                    assignedTo = UserModel.GetOrCreateUser(createdBySPUserID, siteCollectionId, serverRelativeUrl + "/" + projectSiteName);

                    work = new Work();
                    work.WorkItemType = WorkType.Task;
                    if (title.Length > 255)
                        title = title.Substring(0, 255);
                    work.Title = title;
                    List<int> assignedToList = new List<int>();
                    assignedToList.Add(assignedTo.Id);
                    work.AssignedToList = assignedToList;
                    work.EndDate = dueDate;
                    work.PercentageComplete = 0;
                    work.CreatedBy = createdBy;                    
                    work.CreatedDate = createdDate;
                    work.ModifiedBy = createdBy;
                    work.ModifiedDate = createdDate;
                    work.ServerRelativeUrl = serverRelativeUrl + "/" + projectSiteName;
                    work.SiteCollectionId = siteCollectionId;
                    work.StatusNote = "";
                    work.EditType = EditType.Created;


                    work = WorkModel.Save(work, siteCollectionId, serverRelativeUrl + "/" + projectSiteName);
                    taskID = work.Id;

                    //if this is more than 0% complete, call an update and set the modified by to the assigned to.
                    if (percentComplete > 0)
                    {
                        work.ModifiedBy = modifiedBy;
                        work.ModifiedDate = modifiedDate;
                        work.PercentageComplete = percentComplete;
                        work.EditType = EditType.Updated;
                        work = WorkModel.Save(work, siteCollectionId, serverRelativeUrl + "/" + projectSiteName);
                    }

                }
            }
            catch (Exception ex)
            {
                status.ExceptionOccurred = true;
                status.Exception = ex.ToString();
                status.Success = false;
            }

        }
        
    }


}
